package com.demo.jingxi.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.jingxi.common.IsDeleted;
import com.demo.jingxi.common.Result;
import com.demo.jingxi.dao.JxStockMapper;
import com.demo.jingxi.pojo.JxStock;
import com.demo.jingxi.service.JxStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/stock")
public class JxStockController {
    @Autowired
    private JxStockService jxStockService;
    @Autowired
    private JxStockMapper jxStockMapper;
    @Transactional
    @GetMapping("/List")
    public Result<Map<String,Object>> getAllStock(@RequestParam(value = "pdname", required = false) String pdname,

                                                  @RequestParam(value = "pageNo" ) Long pageNo,
                                                  @RequestParam(value = "pageSize") Long pageSize){
        if (log.isInfoEnabled()) {
            log.info("JxStockController.getAllStock.pdname:{}", JSON.toJSONString(pdname));
        }





        String s = decodeParameter(pdname);
        LambdaQueryWrapper<JxStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(pdname), JxStock::getPdname,pdname);


        Page<JxStock> page = new Page<>(pageNo, pageSize);
        jxStockService.page(page,wrapper);

        Map<String,Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }
    private String decodeParameter(String parameter) {
        if (parameter != null && !parameter.isEmpty()) {
            try {
                return URLDecoder.decode(parameter, "UTF-8");
            } catch (Exception e) {
                // 解码失败则返回原始参数值
            }
        }
        return parameter;
    }
    @DeleteMapping("/{number}")
    public Result<JxStock> delete(@PathVariable("number") Integer number){
        LambdaQueryWrapper<JxStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JxStock::getNumber,number);
        JxStock jxStock = jxStockService.getOne(wrapper);
        jxStock.setIsdeleted(IsDeleted.DELETED.getCode());
         jxStockService.updateById(jxStock);
        return Result.success("删除角色成功");
    }
}
